package com.example.demo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtils {
    public static File zipFiles(List<File> fileList, File zipFile) {
        //输出流 最终写文件的流
        FileOutputStream fileOutputStream = null;

        ZipOutputStream zipOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(zipFile);
            zipOutputStream = new ZipOutputStream(fileOutputStream);

            for(File file : fileList){
                //文件转为输入流
                FileInputStream fileInputStream = new FileInputStream(file);

                ZipEntry zipEntry = new ZipEntry(file.getName());

                zipOutputStream.putNextEntry(zipEntry);

                while (true){
                    byte[] bytes = new byte[1024];

                    int readCount = fileInputStream.read(bytes);

                    if(readCount == -1){
                        break;
                    }
                    zipOutputStream.write(bytes, 0, readCount);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(zipOutputStream != null){
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return zipFile;
    }

    /**
     * 断点续传
     * @param file
     * @param src
     * @return
     */
    public static File copyFile(File file, String src, Long position){

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        boolean copyInterrupt = false;

        File targetFile = new File(src);
        if(!targetFile.exists()){
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            copyInterrupt = true;
        }

        try {
            fileInputStream = new FileInputStream(file);

            fileOutputStream = new FileOutputStream(targetFile,true);
            while (true){
                //传文件
                byte[] bytes = new byte[4];

                //如果文件中断过  进行续传
                if(copyInterrupt){
                    fileInputStream.skip(position);
                }

                int read = fileInputStream.read(bytes);
                if (read == -1){
                    break;
                }
//                if (read != 4){
//                    throw new FileNotFoundException();
//                }

                fileOutputStream.write(bytes);
            }
            fileOutputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
        return targetFile;
    }


}
